const CONFIG = require("../../config")
const knex = require('knex')(CONFIG.mysql);
module.exports = {
    /*
    如何获取值 get请求方法   获取req的query方法取值
               post请求方法  获取req的body方法取值
    */

    async getCheckList(req, res) { //审核列表
        //获取url值
        //const { current, size } = req.query;
        knex('community_check_tab').select().then(resp => {
            //密码加密
            if (resp.length > 0) {
                items = {
                    code: 200,
                    data: resp,
                    msg: "操作成功",
                    success: true
                }
                res.send(items)
            } else {
                items = {
                    code: 200,
                    data: resp,
                    msg: "error",
                    success: true
                }
                res.send(items);
            }
        })
    },
    async postRemoveCheck(req, res) { //删除审核
        console.log("res._parsedUrl", req.query.id);
        const { id, comment_id } = req.query;

        knex('community_check_tab').delete().where({
            id: id
        }).then(resp => {
            if (resp) {
                knex('user_cloud_tab').update({
                    isshaw_comment: 0
                }).where({
                    id: comment_id
                }).then((resq) => {
                    if (resq) {
                        res.send({
                            code: 200,
                            msg: "操作成功",
                            success: true
                        })
                    } else {
                        res.send({
                            code: 4001,
                            msg: "操作失败",
                            success: true
                        })
                    }
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },

    async postNewCommunity(req, res) { //新增---审核通过
        let { id, user_id, cloud_id, title, imgList, create_time } = req.body;
        console.log("---req.body--", req.body)
        if (imgList === null || imgList.length === 0) {
            imgList = null
        }
        knex('community_tab').insert({
            id: id,
            user_id: user_id,
            title: title,
            imgList: imgList,
            cloud_id: cloud_id,
            comment_id: cloud_id,
            create_time: create_time,
            likeNum: 0,
            commentNum: 0,
        }).then(resp => {
            console.log("res", resp);
            if (resp) {
                knex('community_check_tab').delete().where({
                    id: id
                }).then(resq => {
                    if (resq) {
                        res.send({
                            code: 200,
                            msg: "操作成功",
                            success: true
                        })
                    } else {
                        res.send({
                            code: 4001,
                            msg: "操作失败",
                            success: true
                        })
                    }
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },

    async getCommunityList(req, res) { //社区列表
        //获取url值
        //const { current, size } = req.query;
        console.log("---req.data2---", req.query)
        let dataCount;
        if (req.query.user_id) {
            knex('community_tab').select(
                'community_tab.id',
                "user_id",
                "cloud_id",
                "avatarUrl",
                "like_community_ids",
                "like_user_ids",
                'title',
                'likeNum',
                'commentNum',
                'imgList',
                'updata_time',
                'create_time',
                'username',
                'nickName',
            ).orderBy(
                'id', 'decs'
            ).limit(
                req.query.limit
            ).leftJoin(
                'user_tab',
                'community_tab.user_id', 'user_tab.id'
            ).where({
                user_id: req.query.user_id
            }).then(resp => {
                //console.log("resp---", resp)
                if (resp.length > 0) {
                    items = {
                        code: 200,
                        data: resp,
                        dataCount: resp.length,
                        msg: "操作成功",
                        success: true
                    }
                    res.send(items)
                } else {
                    res.send({
                        code: 2001,
                        data: [],
                        dataCount: resp.length,
                        msg: "操作失败！",
                        success: false
                    });
                }
            })

        } else {
            knex('community_tab').select(
                'community_tab.id',
                "user_id",
                "cloud_id",
                "avatarUrl",
                "like_community_ids",
                "like_user_ids",
                'title',
                'likeNum',
                'commentNum',
                'imgList',
                'updata_time',
                'create_time',
                'username',
                'nickName',
            ).orderBy(
                'id', 'decs'
            ).limit(
                req.query.limit
            ).leftJoin(
                'user_tab',
                'community_tab.user_id', 'user_tab.id'
            ).then(resp => {
                //console.log("resp---", resp)
                if (resp.length > 0) {
                    items = {
                        code: 200,
                        data: resp,
                        dataCount: resp.length,
                        msg: "操作成功",
                        success: true
                    }
                    res.send(items)
                } else {
                    res.send({
                        code: 2001,
                        data: [],
                        dataCount: resp.length,
                        msg: "操作失败！",
                        success: false
                    });
                }
            })
        }
    },
    //社区删除
    async postRemoveCommunity(req, res) {
        console.log("res._parsedUrl", req.body);
        const { id, comment_id } = req.body;
        //清除社区数据、评论数据、修改user_cloud是否发布字段
        knex('community_tab').delete().where({ cloud_id: id }).then(resp => {
            if (resp) {
                knex('comment_tab').delete().where({ community_id: comment_id }).then(resq => {
                    knex('user_cloud_tab').update({ isshaw_comment: 0 }).where({ id: id }).then(() => {
                        if (resq) {
                            res.send({
                                code: 200,
                                msg: "社区删除成功",
                                success: true
                            })
                        } else {
                            res.send({
                                code: 200,
                                msg: "取消发布成功",
                                success: true
                            })
                        }
                    })
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },
    //社区同步云
    async postUpdateCommunity(req, res) {
        console.log("req.id-comment_id--", req.body.id, req.body.cloud_id);
        const { id, cloud_id, updata_time } = req.body;
        //查询cloud数据，更新community里title\imgList数据
        knex('user_cloud_tab').select().where({
            id: cloud_id
        }).then(resp => {
            console.log("---resp---", req.body);
            knex('community_tab').update({
                title: resp[0].title,
                imgList: resp[0].imgList,
                updata_time: updata_time,
            }).where({
                id: id
            }).then(resq => {
                if (resq) {
                    res.send({
                        code: 200,
                        msg: "操作成功",
                        success: true
                    })
                } else {
                    res.send({
                        code: 4001,
                        msg: "操作失败",
                        success: true
                    })
                }
            })
        })
    }
}